package com.example.panpan.productManage.controller;

import com.example.common.utils.IMap;
import com.example.common.utils.PageView;
import com.example.common.utils.ReturnMapUtils;
import com.example.common.utils.SessionUtil;
import com.example.panpan.productManage.service.ProductService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class productCon {
    @Autowired
    private ProductService productService;

    /**
     * 跳转产品管理页面
     * */
    @RequestMapping(value = "/productManage/toPage/productManageView")
    public String productManageView(){
        return "/module/productManage/productManage";
    }
    /**
     * 跳转库存管理页面
     * */
    @RequestMapping(value = "/productManage/toPage/stockManageView")
    public String stockManageView(){
        return "/module/productManage/stockManage";
    }
    /**
     * 跳转库存变动历史页面
     * */
    @RequestMapping(value = "/productManage/toPage/stockChangeHisView")
    public String stockChangeHisView(){
        return "/module/productManage/stockChangeHistory";
    }
    /**
     * 跳转产品展示页面
     * */
    @RequestMapping(value = "/productManage/toPage/productView")
    public String productView(){
        return "/module/productManage/productView";
    }
    /**
     * 获得产品列表
     * */
    @RequestMapping(value = "/productManage/ajax/getProductList",method = RequestMethod.POST)
    @ResponseBody
    public Map getProductList(HttpServletRequest request){
        IMap params = new IMap(request);
        Page page = PageView.startPageForTable(params, false);
        List<IMap> productList = productService.getProductList(params);
        Map<String, Object> result = PageView.getResultForTable(page, productList);
        return result;
    }
    /**
     * 跳转添加产品页面
     * */
    @RequestMapping(value = "/productManage/toPage/productAddView")
    public String productAddView(){
        return "/module/productManage/addProduct";
    }
    /**
     * 添加产品
     * */
    @RequestMapping(value = "/productManage/ajax/addProduct",method = RequestMethod.POST)
    @ResponseBody
    public Map addProduct(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = productService.addProduct(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","添加成功！");
        }else if (i==0){
            return ReturnMapUtils.businessResult("9999","此编号产品已在售，不可重复录入！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，添加失败！");
        }
    }
    /**
     * 跳转修改产品页面
     * */
    @RequestMapping(value = "/productManage/toPage/productUpdView")
    public String productUpdView(HttpServletRequest request,ModelMap modelMap){
        IMap params = new IMap(request);
        IMap thisProduct = productService.getProductInfo(params);
        modelMap.addAttribute("thisProduct",thisProduct);
        return "/module/productManage/updProduct";
    }
    /**
     * 修改产品信息
     * */
    @RequestMapping(value = "/productManage/ajax/updProduct",method = RequestMethod.POST)
    @ResponseBody
    public Map updProduct(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = productService.updProduct(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","修改成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，修改失败！");
        }
    }
    /**
     * 废止产品信息
     * */
    @RequestMapping(value = "/productManage/ajax/delProduct",method = RequestMethod.POST)
    @ResponseBody
    public Map delProduct(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = productService.delProduct(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","废止成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，操作失败！");
        }
    }
    /**
     * 启用产品信息
     * */
    @RequestMapping(value = "/productManage/ajax/doProductIn",method = RequestMethod.POST)
    @ResponseBody
    public Map doProductIn(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = productService.doProductIn(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","操作成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，操作失败！");
        }
    }
    /**
     * 获得库存变动历史
     * */
    @RequestMapping(value = "/productManage/ajax/getStockChangeList",method = RequestMethod.POST)
    @ResponseBody
    public Map getStockChangeList(HttpServletRequest request){
        IMap params = new IMap(request);
        Page page = PageView.startPageForTable(params, true);
        List<IMap> productList = productService.getStockChangeList(params);
        Map<String, Object> result = PageView.getResultForTable(page, productList);
        return result;
    }
    /**
     * 增加库存
     * */
    @RequestMapping(value = "/productManage/ajax/addStock",method = RequestMethod.POST)
    @ResponseBody
    public Map addStock(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = productService.addStock(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","操作成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，操作失败！");
        }
    }
    /**
     * 产品报表相关
     * */
    @RequestMapping("/report/productMessage")
    public String toPageStaffManage(){
        return "/module/report/productMessage";
    }

    @RequestMapping("/report/ajax/getProductMessage")
    @ResponseBody
    public Map getMessage(HttpServletRequest request){
        IMap params = new IMap(request);
        Page page = PageView.startPageForTable(params, false);
        List res = productService.getProductList(params);
        Map<String, Object> result = PageView.getResultForTable(page, res);
        return result;
    }
}
